﻿using System;
using System.Collections.Generic;
using System.IO;
using Autofac;
using AutoMapper;
using SXICE.Core;
using SXICE.Core.Configs;
using SXICE.Core.Helpers;
using SXICE.Core.Infrastructure;

namespace SXICE.Core
{
    /// <summary>
    /// 启动
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 所有的模块
        /// </summary>
        private static readonly List<IModule> modules = new List<IModule>();

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns>容器</returns>
        public static ContainerBuilder Register()
        {
            var containerBuilder = new ContainerBuilder();
            var configContent = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"));
            containerBuilder.RegisterInstance(ConvertHelper.JsonToObject<CacheConfigs>(configContent));
            containerBuilder.RegisterInstance(ConvertHelper.JsonToObject<DBConfigs>(configContent));
            containerBuilder.RegisterInstance(ConvertHelper.JsonToObject<GlobalConfigs>(configContent));
            //containerBuilder.RegisterInstance(ConvertHelper.JsonToObject<WeChatAPIConfigs>(configContent));
            //containerBuilder.RegisterInstance(ConvertHelper.JsonToObject<SMSConfigs>(configContent));
            containerBuilder.RegisterInstance(ConvertHelper.JsonToObject<SystemConfigs>(configContent));
            containerBuilder.RegisterType<DefaultLocalCache>().As<ILocalCache>().SingleInstance();  //注册本地缓存
            containerBuilder.RegisterType<DefaultDistributedCache>().As<IDistributedCache>().SingleInstance();  //注册分布式缓存
            containerBuilder.RegisterType<DefaultLog>().As<ILog>().SingleInstance();    //注册默认日志
            //注册模块
            foreach (var module in RuntimeHelper.Resolves<IModule>())
                //需要保证每个模块的加载不会影响整个程序的运行
                try
                {
                    module.Register(containerBuilder);
                    modules.Add(module);    //模块正常则添加
                }
                catch { }
            //注册Mapper文件
            var mapperConfiguration = new MapperConfiguration(configure =>
            {
                foreach (var mapper in RuntimeHelper.Resolves<IMapperRegister>())
                    try
                    {
                        mapper.Register(configure);
                    }
                    catch { }
            });
            containerBuilder.RegisterInstance(mapperConfiguration.CreateMapper());
            RuntimeHelper.SetContainer(containerBuilder);
            return containerBuilder;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            //初始化模块
            modules.ForEach(module =>
            {
                try
                {
                    module.Init();
                }
                catch { }
            });
        }

        /// <summary>
        /// 启动
        /// </summary>
        public static void Start()
        {
            //启动模块
            modules.ForEach(module =>
            {
                try
                {
                    module.Start();
                }
                catch { }
            });
        }

        /// <summary>
        /// 停止
        /// </summary>
        public static void Stop()
        {
            //启动模块
            modules.ForEach(module =>
            {
                try
                {
                    module.Stop();
                }
                catch { }
            });
        }
    }
}
